import os
from PIL import Image

current_path = 'webp'


def convert_image(img_list):
    for webp in img_list:  # 循环webp文件列表
        print(webp)
        ext = os.path.splitext(webp)[1]  # 获取webp文件的名字
        print(ext)
        if ext != '.webp':
            continue  # 如果文件后缀不是webp则跳过本文件
        print(webp)
        img_name = webp[0:webp.rindex('.')]  # 获取‘.’之前的内容-图片名称

        webp_path = os.path.join(current_path, webp)  # 拼接webp图片路径

        img = Image.open(webp_path)  # webp图片路径
        img.load()  # 打开webp图片  得到webp图片数据

        jpg_path = os.path.join(current_path, img_name+'.png')  # jpg图片路径

        img.save(jpg_path)  # webp图片数据写入jpg文件

        os.remove(webp_path)  # 删除webp文件


def start_convert():
    img_list = []
    for root, dirs, files in os.walk(current_path):
        for f in files:
            img_list.append(f)
    convert_image(img_list)

if __name__ == '__main__':
    start_convert()